package cn.funeralobjects.util;

import cn.funeralobjects.util.enums.SystemProperties;
import cn.funeralobjects.util.model.SystemInfo;

/**
 * 系统工具
 *
 * @author FuneralObjects
 * Create date: 2020/4/14 11:14 AM
 */
public class SystemUtils {

    private static final SystemInfo SYSTEM_INFO;
    private static final String WINDOWS_PREFIX = "Windows";
    private static final String WINDOWS_DESKTOP = "windows";
    private static final String MAC_PREFIX = "Mac";
    private static final boolean ON_WINDOWS;
    private static final boolean ON_MAC;

    static {
        SYSTEM_INFO = new SystemInfo(
                getProperty(SystemProperties.JAVA_VERSION),
                getProperty(SystemProperties.JAVA_HOME),
                getProperty(SystemProperties.OS_NAME),
                getProperty(SystemProperties.OS_VERSION),
                getProperty(SystemProperties.FILE_ENCODING),
                getProperty(SystemProperties.LINE_SEPARATOR),
                getProperty(SystemProperties.PATH_SEPARATOR),
                getProperty(SystemProperties.FILE_SEPARATOR),
                getProperty(SystemProperties.IO_UNICODE_ENCODING),
                getProperty(SystemProperties.ENCODING),
                getProperty(SystemProperties.USER_HOME),
                getProperty(SystemProperties.USER_COUNTRY),
                getProperty(SystemProperties.OS_ARCH)
        );

        ON_WINDOWS = SYSTEM_INFO.getOsName().startsWith(WINDOWS_PREFIX) && WINDOWS_DESKTOP.equals(System.getProperty("sun.desktop"));
        ON_MAC = !ON_WINDOWS && SYSTEM_INFO.getOsName().startsWith(MAC_PREFIX);
    }


    /**
     * 判断是否为在 windows 系统内
     *
     * @return true - 为 windows 系统
     */
    public static boolean isOnWindows() {
        return ON_WINDOWS;
    }

    /**
     * 判断是否在 mac 系统内
     *
     * @return true - 为 mac 系统
     */
    public static boolean isOnMac() {
        return ON_MAC;
    }

    /**
     * 获取系统信息
     *
     * @return 系统信息对象
     */
    public static SystemInfo systemInfo() {
        return SYSTEM_INFO;
    }

    private static String getProperty(SystemProperties property) {
        return System.getProperty(property.getKey());
    }
}
